package com.yl.immodule.fragment.model;

import androidx.lifecycle.LiveData;
import androidx.lifecycle.MutableLiveData;

import com.android.yl.ylicpsdk.ICPRtc;
import com.android.yl.ylicpsdk.ICPRtcEventHandler;
import com.android.yl.ylicpsdk.ICPRtcManager;
import com.camera.base.data.NumberPickerShow;
import com.camera.base.v.models.BaseViewModel;
import com.yl.immodule.R;
import com.yl.immodule.util.MMkvSettingUtil;

public class ChannelSettingViewModel extends BaseViewModel {
    private final MutableLiveData<String> _agc = new MutableLiveData<>();
    public LiveData<String> agc = _agc;
    private final MutableLiveData<String> _aec = new MutableLiveData<>();
    public LiveData<String> aec = _aec;
    private final MutableLiveData<String> _ng = new MutableLiveData<>();
    public LiveData<String> ng = _ng;
    private final MutableLiveData<NumberPickerShow> _showNumberPickerDialog = new MutableLiveData<>();
    public LiveData<NumberPickerShow> showNumberPickerDialog = _showNumberPickerDialog;
    public int[] agcArray = new int[]{0,1,2,3,4,5,6,7,8,9,10,11,12};
    public int[] aecArray = new int[]{-1,1,2,3};
    public int[] ngArray = new int[]{-1,1,2,3};
    private boolean agcEnable = false, aecEnable = false, ngEnable = false;
    
    public ChannelSettingViewModel() {
        if (MMkvSettingUtil.INSTANCE.getAgc() == 0){
            _agc.setValue("关闭");
            agcEnable = false;
        } else {
            agcEnable = true;
            _agc.setValue(MMkvSettingUtil.INSTANCE.getAgc()+"");
        }
        if (MMkvSettingUtil.INSTANCE.getAec() == -1){
            _aec.setValue("关闭");
            aecEnable = false;
        }else{
            _aec.setValue(MMkvSettingUtil.INSTANCE.getAec()+"");
            aecEnable = true;
        }
        if (MMkvSettingUtil.INSTANCE.getNg() == -1){
            _ng.setValue("关闭");
            ngEnable = false;
        }else{
            _ng.setValue(MMkvSettingUtil.INSTANCE.getNg()+"");
            ngEnable = true;
        }
    }
    
    public void showNumberPickerDialog(int viewId){
        String[] options = null;
        if (viewId == R.id.agc){
            options = new String[agcArray.length];
            for (int i = 0; i < agcArray.length; i++) {
                if (agcArray[i] == 0){
                    options[i] = "关闭";
                }else{
                    options[i] = agcArray[i]+"";
                }
            }
        } else if (viewId == R.id.aec){
            options = new String[aecArray.length];
            for (int i = 0; i < aecArray.length; i++) {
                if (aecArray[i] == -1){
                    options[i] = "关闭";
                }else{
                    options[i] = aecArray[i]+"";
                }
            }
        } else if (viewId == R.id.ng){
            options = new String[ngArray.length];
            for (int i = 0; i < ngArray.length; i++) {
                if (ngArray[i] == -1){
                    options[i] = "关闭";
                }else{
                    options[i] = ngArray[i]+"";
                }
            }
        }
        NumberPickerShow numberPickerShow = new NumberPickerShow(viewId,true,options);
        _showNumberPickerDialog.setValue(numberPickerShow);
    }

    //ICPRtcEventHandler.AudioAecLevel.AEC_LEVEL_3;
    public void saveAgc(int choose){
        MMkvSettingUtil.INSTANCE.setAgc(agcArray[choose]);
        if (agcArray[choose] == 0) _agc.setValue("关闭"); else _agc.setValue(agcArray[choose]+"");
        ICPRtc.Configure configure = ICPRtcManager.getsInstance().getConfig();
        if (agcArray[choose] == 0){
            _agc.setValue("关闭");
            agcEnable = false;
            if (!aecEnable && !ngEnable) {
                configure.aiNoiseSuppression = false;
            }
        } else {
            agcEnable = true;
            configure.aiNoiseSuppression = true;
        }
//        configure.audioAecLevel = ICPRtcEventHandler.AudioAecLevel.AEC_LEVEL_3;
        configure.audioAgcLevel = agcArray[choose];
//        configure.audioNsLevel = ICPRtcEventHandler.AudioNsLevel.NS_LEVEL_3;
        ICPRtcManager.getsInstance().config(configure);
    }
    
    public void saveAec(int choose){
        MMkvSettingUtil.INSTANCE.setAec(aecArray[choose]);
        if (aecArray[choose] == -1) _aec.setValue("关闭"); else _aec.setValue(aecArray[choose]+"");
        ICPRtc.Configure configure = ICPRtcManager.getsInstance().getConfig();
        if (aecArray[choose] == -1){
            _aec.setValue("关闭");
            aecEnable = false;
            if (!agcEnable && !ngEnable) {
                configure.aiNoiseSuppression = false;
            }
        } else {
            aecEnable = true;
            configure.aiNoiseSuppression = true;
        }
        configure.audioAecLevel = aecArray[choose];
        ICPRtcManager.getsInstance().config(configure);
    }
    
    public void saveNg(int choose){
        MMkvSettingUtil.INSTANCE.setNg(ngArray[choose]);
        if (ngArray[choose] == -1) _ng.setValue("关闭"); else _ng.setValue(ngArray[choose]+"");
        ICPRtc.Configure configure = ICPRtcManager.getsInstance().getConfig();
        if (ngArray[choose] == -1){
            _ng.setValue("关闭");
            ngEnable = false;
            if (!agcEnable && !aecEnable) {
                configure.aiNoiseSuppression = false;
            }
        } else {
            ngEnable = true;
            configure.aiNoiseSuppression = true;
        }
        configure.audioNsLevel = ngArray[choose];
        ICPRtcManager.getsInstance().config(configure);
    }
}
